package com.thread;

import org.omg.PortableInterceptor.INACTIVE;

import java.util.concurrent.TimeUnit;

/**
 * 懒汉式  并发问题
 */
public class ThreadSingleClass008 {
    public static ThreadSingleClass008 INSTANCE;

    //synchronized 这里锁粒度太重，有没有更好优化方法
    public static /*synchronized*/ ThreadSingleClass008 getInstance() {
        if(INSTANCE == null){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            INSTANCE = new ThreadSingleClass008();
        }
        return INSTANCE;
    }
    private ThreadSingleClass008(){}

    public static void main(String[] args) {
        for(int i=0;i<100;++i){
            new Thread(()->{
                System.out.println(ThreadSingleClass008.getInstance().hashCode());;
            }).start();
        }
    }
}
